﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ISay_TelaLogin
{
    public partial class telaConversa : Form
    {
        private string usuario;
        private SqlConnection conexao;
        private Button novobotao;


        public telaConversa(string user)
        {
            usuario = user;
            InitializeComponent();
        }

        private void telaConversa_Load(object sender, EventArgs e)
        {
            string connectionString = Properties.Settings.Default.iSayConnectionString;
            conexao = new SqlConnection(connectionString);
            conexao.Open();

            SqlCommand comandosql = new SqlCommand("SELECT Grupo FROM Participacao WHERE usuario = '" + usuario + "'", conexao);
            SqlDataReader dataReader = comandosql.ExecuteReader();
            int y = 3;
            int cont = 0;

            while (dataReader.Read())
            {
                SqlCommand comandosql2 = new SqlCommand("SELECT nome FROM Grupo WHERE id_grupo = " + dataReader["Grupo"].ToString(), conexao);
                SqlDataReader dataReaderNome = comandosql2.ExecuteReader();

                if (dataReaderNome.Read())
                {
                    novobotao = new Button();
                    novobotao.Size = new Size(174, 35);
                    novobotao.Location = new Point(3, y);
                    novobotao.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
                    novobotao.Font = new System.Drawing.Font("Franklin Gothic Book", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    novobotao.Name = "button_"+ cont.ToString();
                    novobotao.Text = dataReaderNome["nome"].ToString();
                    novobotao.Tag = dataReader["Grupo"].ToString();
                    novobotao.Click += new EventHandler (novobotao_Click);
                    panel1.Controls.Add(novobotao);
                    y += 38;
                }     

                cont++;
                dataReaderNome.Close();
            }

            dataReader.Close();
        }

        private void novobotao_Click(object sender, EventArgs e)
        {
            Button currentButton = (Button)sender;
            MessageBox.Show(currentButton.Tag.ToString());

        }
    }
}
